<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="所属店铺ID" prop="storeId">
        <el-input v-model="dataForm.storeId" placeholder="所属店铺ID"></el-input>
      </el-form-item>
      <el-form-item label="所属分类ID" prop="classifyId">
        <el-input v-model="dataForm.classifyId" placeholder="所属分类ID"></el-input>
      </el-form-item>
      <el-form-item label="所属目录ID" prop="categoryId">
        <el-input v-model="dataForm.categoryId" placeholder="所属目录ID"></el-input>
      </el-form-item>
      <el-form-item label="商品名称" prop="produceName">
        <el-input v-model="dataForm.produceName" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-form-item label="商品价格" prop="producePrice">
        <el-input v-model="dataForm.producePrice" placeholder="商品价格"></el-input>
      </el-form-item>
      <el-form-item label="商品描述" prop="produceDesc">
        <el-input v-model="dataForm.produceDesc" placeholder="商品描述"></el-input>
      </el-form-item>
      <el-form-item label="商品封面图" prop="produceIcon">
        <!-- <el-input v-model="dataForm.produceIcon" placeholder="商品封面图"></el-input> -->
        <single-upload v-model="dataForm.produceIcon"></single-upload>
      </el-form-item>
      <el-form-item label="商品状态" prop="produceStatus">
        <el-input v-model="dataForm.produceStatus" placeholder="商品状态"></el-input>
      </el-form-item>
      <el-form-item label="商品库存" prop="produceStock">
        <el-input v-model="dataForm.produceStock" placeholder="商品库存"></el-input>
      </el-form-item>
      <el-form-item label="商品详情图" prop="produceImgs">
        <!-- <el-input v-model="dataForm.produceImgs" placeholder="商品详情图"></el-input> -->
        <single-upload v-model="dataForm.produceImgs"></single-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from "@/components/upload/singleUpload";
export default {
  components: { SingleUpload },
  data() {
    return {
      visible: false,
      dataForm: {
        produceId: 0,
        storeId: "",
        classifyId: "",
        categoryId: "",
        produceName: "",
        producePrice: "",
        produceDesc: "",
        produceIcon: "",
        produceStatus: "",
        produceStock: "",
        produceImgs: "",
      },
      dataRule: {
        storeId: [
          { required: true, message: "所属店铺ID不能为空", trigger: "blur" },
        ],
        classifyId: [
          { required: true, message: "所属分类ID不能为空", trigger: "blur" },
        ],
        categoryId: [
          { required: true, message: "所属目录ID不能为空", trigger: "blur" },
        ],
        produceName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" },
        ],
        producePrice: [
          { required: true, message: "商品价格不能为空", trigger: "blur" },
        ],
        produceDesc: [
          { required: true, message: "商品描述不能为空", trigger: "blur" },
        ],
        produceIcon: [
          { required: true, message: "商品封面图不能为空", trigger: "blur" },
        ],
        produceStatus: [
          { required: true, message: "商品状态不能为空", trigger: "blur" },
        ],
        produceStock: [
          { required: true, message: "商品库存不能为空", trigger: "blur" },
        ],
        produceImgs: [
          { required: true, message: "商品详情图不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.produceId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.produceId) {
          this.$http({
            url: this.$http.adornUrl(
              `/produce/produce/info/${this.dataForm.produceId}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.storeId = data.produce.storeId;
              this.dataForm.classifyId = data.produce.classifyId;
              this.dataForm.categoryId = data.produce.categoryId;
              this.dataForm.produceName = data.produce.produceName;
              this.dataForm.producePrice = data.produce.producePrice;
              this.dataForm.produceDesc = data.produce.produceDesc;
              this.dataForm.produceIcon = data.produce.produceIcon;
              this.dataForm.produceStatus = data.produce.produceStatus;
              this.dataForm.produceStock = data.produce.produceStock;
              this.dataForm.produceImgs = data.produce.produceImgs;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/produce/produce/${!this.dataForm.produceId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              produceId: this.dataForm.produceId || undefined,
              storeId: this.dataForm.storeId,
              classifyId: this.dataForm.classifyId,
              categoryId: this.dataForm.categoryId,
              produceName: this.dataForm.produceName,
              producePrice: this.dataForm.producePrice,
              produceDesc: this.dataForm.produceDesc,
              produceIcon: this.dataForm.produceIcon,
              produceStatus: this.dataForm.produceStatus,
              produceStock: this.dataForm.produceStock,
              produceImgs: this.dataForm.produceImgs,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
